package com.zwan.oam_rtc.utils.messages;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

/*typedef struct
{
    UINT16  messageFlag;
    UINT16  version;
    UINT16     nTurn;
    UINT16     dlgID;
} SDSMsgHead;*/
public class SDSMsgHead extends AbstractStruct {
    public static final int LENGTH = 8;
    private short messageFlag;
    private short version;
    private short nTurn;
    private short dlgID;
    // 自增会话号，一共 65536个会话，理论上不会重复
    private static short dlgid = Short.MIN_VALUE;
    
    public SDSMsgHead(){
//    	dlgID = getNextDlgId();
    }
    
	public synchronized static short getNextDlgId() {
    	if (dlgid == Short.MAX_VALUE)
    		dlgid = Short.MIN_VALUE;
    	else
    		dlgid ++;	
    	return dlgid;
    }

    public void writeToBuffer(ByteBuffer bf) {
        bf.putShort(messageFlag);
        bf.putShort(version);
        bf.putShort(nTurn);
        bf.putShort(dlgID);
    }
    
    public void readFromBuffer(ByteBuffer bf){
    	this.messageFlag = bf.order(ByteOrder.LITTLE_ENDIAN).getShort();
        this.version = bf.order(ByteOrder.LITTLE_ENDIAN).getShort();
        this.nTurn = bf.order(ByteOrder.LITTLE_ENDIAN).getShort();
        this.dlgID = bf.order(ByteOrder.LITTLE_ENDIAN).getShort();
    }
    
    public void cloneFromObject(SDSMsgHead head){
    	this.messageFlag = head.messageFlag;
    	this.version = head.version;
    	this.nTurn = head.nTurn;
    	this.dlgID = head.dlgID;
    }

    public int getLength() {
        return LENGTH;
    }
    

    public short getDlgID() {
        return dlgID;
    }

    public short getMessageFlag() {
        return messageFlag;
    }

    public short getNTurn() {
        return nTurn;
    }

    public short getVersion() {
        return version;
    }

    public void setDlgID(short dlgID) {
        this.dlgID = dlgID;
    }

    public void setMessageFlag(short messageFlag) {
        this.messageFlag = messageFlag;
    }

    public void setNTurn(short turn) {
        nTurn = turn;
    }

    public void setVersion(short version) {
        this.version = version;
    }

	
}
